package com.yilin.tms.capital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.tms.capital.mapper.BankCardMapper;
import com.yilin.tms.capital.mapper.PayChannelMapper;
import com.yilin.tms.capital.service.IBankCardService;
import com.yilin.commons.exception.BusinessException;
import com.yilin.tms.core.entity.capital.BankCard;
import com.yilin.tms.core.entity.capital.PayChannel;
import com.yilin.tms.core.entity.user.OrganType;
import com.yilin.tms.core.entity.user.account.Account;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class BankCardService extends ServiceImpl<BankCardMapper, BankCard> implements IBankCardService {

    @Resource
    BankCardMapper bankCardMapper;
    @Resource
    PayChannelMapper payChannelMapper;

    @Override
    public BankCard getBackCardById(String id) {
        return bankCardMapper.selectById(id);
    }

    @Override
    public BankCard getBackCardByNo(String bankAccNo) {
        QueryWrapper<BankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bank_acc_no", bankAccNo);
        queryWrapper.eq("is_deleted", 0);
        return bankCardMapper.selectOne(queryWrapper);
    }

    @Override
    public BankCard getDefaultBankCardByOrganizeId(String organizeId) {
        QueryWrapper<BankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", organizeId);
        queryWrapper.eq("default_card", true);
        return bankCardMapper.selectOne(queryWrapper);
    }
    @Override
    public List<BankCard> getDefaultBankCardListByOrganizeId(String[] organizeIds) {
        QueryWrapper<BankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("organize_id", Arrays.asList(organizeIds));
        queryWrapper.eq("default_card", true);
        return bankCardMapper.selectList(queryWrapper);
    }

    @Override
    public List<BankCard> getBackCardList(Account loginUser,String shortName) {
        QueryWrapper<BankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", loginUser.getOrganizeId());
        if (shortName!=null) queryWrapper.eq("bank_short_name", shortName);
        return bankCardMapper.selectList(queryWrapper);
    }

    @Override
    public void saveBankCard(Account loginUser, BankCard bankCard) {
        List<BankCard> backCardList = getBackCardList(loginUser,null);
        for (BankCard otherBankCard : backCardList) {
            if (bankCard.getId()==null || !bankCard.getId().equals(otherBankCard.getId())){
                otherBankCard.setDefaultCard(false);
                bankCardMapper.updateById(otherBankCard);
            }
        }
        bankCard.setDefaultCard(true);
        if (bankCard.getId() == null) {
            bankCard.setOrganizeId(loginUser.getOrganizeId());
            bankCard.setOrganizeName(loginUser.getOrganizeName());
            bankCard.setHoldCardName(loginUser.getOrganizeName().replaceAll("（个体）", ""));
            bankCard.setBankAccType(loginUser.getOrganType() != OrganType.individual ? BankCard.BankAccType.businessAcc : BankCard.BankAccType.personCard);
            bankCardMapper.insert(bankCard);
        } else {
            bankCardMapper.updateById(bankCard);
        }
    }


    //--------------------------------------------------------------------------
    @Override
    public void savePayChannel(Account loginUser, PayChannel payChannel) {
        if (payChannel.getId() == null) {
            PayChannel local = getPayChannelByBankType(loginUser, payChannel.getBankType());
            if (local != null) throw new BusinessException("该银行支付通道已存在，请直接编辑！");
            payChannel.setOrganizeId(loginUser.getOrganizeId());
            payChannel.setOrganizeName(loginUser.getOrganizeName());
            payChannelMapper.insert(payChannel);
        } else {
            PayChannel local = payChannelMapper.selectById(payChannel.getId());
            local.setBankCardId(payChannel.getBankCardId());
            local.setBankCardNo(payChannel.getBankName());
            local.setHoldCardName(payChannel.getHoldCardName());
            payChannelMapper.updateById(payChannel);
        }
    }

    @Override
    public PayChannel getPayChannelByBankType(Account loginUser, PayChannel.BankType bankType) {
        QueryWrapper<PayChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", loginUser.getOrganizeId());
        queryWrapper.eq("bank_type", bankType.index);
        return payChannelMapper.selectOne(queryWrapper);
    }

    @Override
    public List<PayChannel> getPayChannelList(Account loginUser) {
        QueryWrapper<PayChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_id", loginUser.getOrganizeId());
        return payChannelMapper.selectList(queryWrapper);
    }

}
